//
//  AOVisitorView.swift
//  weibo_zha
//
//  Created by _zha on 16/10/24.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit

class AOVisitorView: UIView {

    //点击回调
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupVisitorViewInfo(imgName: String?, tilte: String?){
        //
        if let img = imgName, let tit = tilte{
            //设置 image 和 text
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            //隐藏圆圈
            circleImageView.isHidden = true
        }else {
            setupCircleImageViewAnim()
        }
    }
    //设置圆圈动画
    private func setupCircleImageViewAnim(){
        //核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置转的角度
        anim.toValue = M_PI * 2
        //设置时间
        anim.duration = 20
        //重复次数
        anim.repeatCount = MAXFLOAT
        //移除动画
        anim.isRemovedOnCompletion = false
        //添加动画
        circleImageView.layer.add(anim, forKey: nil)
    }
    
    //监听事件
    @objc private func loginClick(){
        //执行闭包
        closure?()
    }
    
    //设置视图
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        //添加约束
        //圆圈
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //遮着
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //logo
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //文字
        messageLabel.snp_makeConstraints { (make) in
            make.width.equalTo(230)
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
        }
        
        //注册按钮
        registerButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        
        //登录按钮
        loginButton.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }
        
    }
    
    //懒加载控件
    //圆圈
    fileprivate lazy var circleImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    //遮罩
    fileprivate lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    //icon
    fileprivate lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    //文字
    fileprivate lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.darkGray, fontSize: AONormalFontSize, text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
        //对齐方式
        lab.textAlignment = .center
        //换行
        lab.numberOfLines = 0
        return lab
    }()
    //注册按钮
    fileprivate lazy var registerButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "注册", fontSize: AONormalFontSize, titleColor: AOHomeColor, target: self, action: #selector(loginClick))
        return button
    }()
    //登录按钮
    fileprivate lazy var loginButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "登录", fontSize: AONormalFontSize, titleColor: AOHomeColor, target: self, action: #selector(loginClick))
        return button
    }()
}
